package com.ivy.utils;

import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.beanutils.BeanUtils;

public class DataReadUtils {
	@Deprecated
	public static <T>T copyBeanOld(HttpServletRequest request, Class<T> clazz) {
		try {
			T t = clazz.newInstance();
			BeanUtils.populate(t, request.getParameterMap());
			return t;
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}		
	}
	
	
	public static <T>T copyBean(HttpServletRequest request, Class<T> clazz) {
		try {
			T t = clazz.newInstance();
			Enumeration<String> enums = request.getParameterNames();
			while(enums.hasMoreElements()) {
				String name = enums.nextElement();
				String value = new String(request.getParameter(name).getBytes("iso-8859-1"),"utf-8");
				BeanUtils.copyProperty(t, name, value);
			}			
			return t;
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}		
	}

}

